//
//  LGTimeView.m
//  SportRing
//
//  Created by mykoma on 14-4-10.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "LGTimeView.h"

@interface LGTimeView ()
{
    LG_TIME_STYLE_TYPE _timeStyle;
    NSInteger _number;
    UIImageView * _tensDigitIV;
    UIImageView * _unitsDigitIV;
    
    NSInteger _maxNumber;
}

@end

@implementation LGTimeView

+(instancetype)timeViewWithStyle:(LG_TIME_STYLE_TYPE)style
                   defaultNumber:(NSInteger)number
{
    return [[LGTimeView alloc] initWithStyle:style defaultNumber:number];
}

+(instancetype)timeViewWithStyle:(LG_TIME_STYLE_TYPE)style
                   defaultNumber:(NSInteger)number
                       withFrame:(CGRect)frame
{
    LGTimeView * timeView = [LGTimeView timeViewWithStyle:style defaultNumber:number];
    timeView.frame = frame;
    return timeView;
}

-(instancetype)initWithStyle:(LG_TIME_STYLE_TYPE)style
               defaultNumber:(NSInteger)number
{
    self = [super init];
    if (self)
    {
        _timeStyle = style;
        _number = number;
        _maxNumber = (_timeStyle == LG_TIME_STYLE_TYPE_HOUR) ? 23 : 59;
        
        [self configNumberView];
    }
    return self;
}

#pragma mark - Public

-(void)increase
{
    _number++;
    if (_number > _maxNumber)
    {
        _number = 0;
    }
    [self configNumberView];
}

-(void)decrease
{
    _number--;
    if (_number < 0)
    {
        _number = _maxNumber;
    }
    [self configNumberView];
}

-(NSInteger)value
{
    return _number;
}

#pragma mark - Private

-(void)configNumberView
{
    _tensDigitIV.image = [UIImage imageNamed:[self imageNameForDigit:_number / 10]];
    _unitsDigitIV.image = [UIImage imageNamed:[self imageNameForDigit:_number % 10]];
}

-(NSString *)imageNameForDigit:(NSInteger)digit
{
    return [NSString stringWithFormat:@"lg_time_%d", digit % 10];
}

#pragma mark - setup

-(void)setupViews
{
    _tensDigitIV = [[UIImageView alloc] init];
    [self addSubview:_tensDigitIV];
    
    _unitsDigitIV = [[UIImageView alloc] init];
    [self addSubview:_unitsDigitIV];
}

-(void)layoutPortrait
{
    _tensDigitIV.frame = CGRectMake(0, 0,
                                    CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds));
    
    _unitsDigitIV.frame = CGRectOffset(_tensDigitIV.frame,
                                       CGRectGetWidth(self.bounds) / 2, 0);
}

@end
